<?php
include('functions.inc.php');
header("Content-type: text/html; charset=UTF-8");
?>
<?php
$link = db_connect();
// In-place edit for Subs titles
if (isset($_GET['editSub'])) {
	$editWhat = substr($_GET['fieldname'], 0, 1) == 't' ? 'title' : 'url';
	$subId = substr($_GET['fieldname'], 2);
	$updateQuery = sprintf("UPDATE subscriptions SET %s = '%s' WHERE id = %d",
		$editWhat,
		mysql_escape_string($_GET['content']),
		mysql_escape_string($subId)
	);
	mysql_query($updateQuery, $link)
		or die("Can't edit subscription: " . mysql_error());

	print $_GET['content'].($editWhat == 'url' ? '<script type="text/javascript">Element.hide("spacer_'.$subId.'");$("subUrl_'.$subId.'").style.overflow="hidden";$("subUrl_'.$subId.'").style.position="relative"</script>' : '');
	exit;
}
?>
<html>
<head>
	<title>GobbleRSS - Subscriptions</title>
	<link href="css/gob.css" type="text/css" rel="stylesheet">
	<script type="text/javascript" src="js/prototype.js"></script>
	<script type="text/javascript">var gob_url='<?php echo $config['URI'] ?>'</script>
	<script type="text/javascript" src="js/gob.js"></script>
	<script type="text/javascript" src="js/boxover.js"></script>
	<script type="text/javascript" src="js/instantedit.js"></script>
	<script type="text/javascript">
		setVarsForm('editSub=1');
		urlBase='subs.php';
	</script>
</head>

<body>

<div style="padding-top: 10px; float: right">
	<?php include ('menu.inc.php') ?>
	<?php if (!isset($_GET['new_sub_ff']) && !isset($_POST['new_sub_ff'])): ?>
		<span style="display:none; padding-top: 10px" id="register_ff_reader">
			<br/><a href="javascript:register_ff_reader();">Add GobbleRSS</a> as a Firefox 2 feed reader.
			<script type="text/javascript">if(window.navigator!=null && window.navigator.registerContentHandler!=null){Element.show('register_ff_reader');}</script>
		</span>
	<?php endif; ?>
	<span style="padding-top: 10px">
		<br/><a href="opml.xml">Export as OPML</a>
	</span>
</div>

<h1>Subscriptions</h1>

<form action="subs.php" method="post" style="padding-top: 7px">
	<div>
		New subscription: 
		<input type="text" name="new_sub" id="new_sub" value="[Title]" onfocus="if(this.value=='[Title]')this.value=''" />
		<input type="text" name="new_sub_url" value="<?php if (isset($_GET['new_sub_ff'])) { echo $_GET['new_sub_ff']; } else { echo "[URL]"; } ?>" onfocus="if(this.value=='[URL]')this.value=''" />
		<input type="submit" style="display:none" />
		<?php if (isset($_GET['new_sub_ff'])): ?>
			<input type="hidden" name="new_sub_ff" value="true" />
		<?php endif; ?>
	</div>
</form>

<?php if (isset($_GET['new_sub_ff'])): ?>
	<span style="padding-bottom: 10px">
		<span class="success">Please enter a title for this new feed.</span>
	</span>
<?php endif; ?>

<?php if (isset($_GET['new_sub_ff'])): ?>
	<script type="text/javascript">
		$('new_sub').focus();
	</script>
<?php endif; ?>

<?php
if (isset($_POST['new_sub'])) {
	$subs_query = "SELECT MAX(id) FROM subscriptions";
	$result = mysql_query($subs_query, $link)
		or die("Can't find max subscriptions ID: " . mysql_error());
	$max_id = mysql_result($result, 0, 0);
	$subs_query = "INSERT INTO subscriptions (id,url,title) VALUES (".($max_id+1).", '".$_POST['new_sub_url']."', '".$_POST['new_sub']."')";
	mysql_query($subs_query, $link)
		or die("Can't create new subscription: " . mysql_error());
	
	?><span class="success">
		<strong><?php echo $_POST['new_sub'] ?></strong> successfully added to your subscriptions.
	</span><br/><?php
}
if (isset($_GET['del'])) {
	$subs_query = "DELETE FROM subscriptions WHERE id='".$_GET['del']."'";
	mysql_query($subs_query, $link)
		or die("Can't delete subscription: " . mysql_error());
	?><span class="success">
		Feed successfully deleted from your subscriptions.
	</span><br/><?php
}

?><table cellpadding="4" cellspacing="0" style="border: 1px solid gray" id="allSubsTable"><?php

$subs_query = "SELECT * FROM subscriptions ORDER BY id";
$result = mysql_query($subs_query, $link)
	or die("Can't load subscriptions: " . mysql_error());

$i = 1;
while ($subscription = mysql_fetch_object($result)) {
	$id = $subscription->id;
	$url = $subscription->url;
	$title = $subscription->title;

	?>
	<tr<?php if ($i++%2==0) { ?> style="background-color: #EEEEEE"<?php } ?>>
		<td><?php echo $id ?> </td>
		<td>
			<span id="t_<?php echo $id ?>" class="editText subTitle"><?php echo $title ?></span><br/>
			<div id="subUrl_<?php echo $id ?>" class="subUrl" onclick="this.style.overflow='visible';Element.show('spacer_<?php echo $id ?>');$('spacer_<?php echo $id ?>').style.height=this.offsetHeight+'px';this.style.position='absolute';"><span id="u_<?php echo $id ?>" class="editText"><?php echo $url ?></span></div>
			<div id="spacer_<?php echo $id ?>" style="display:none">&nbsp;</div>
		</td>
		<td>
			<a href="index.php?sub=<?php echo $id ?>"> articles</a> 
			&nbsp;|&nbsp; <a href="<?php echo $url ?>">feed</a> 
			&nbsp;|&nbsp; <a href="delete/<?php echo $id ?>" onclick="return confirmDelSub(<?php echo $id ?>, '<?php echo str_replace("'","\\'",$title) ?>')">delete</a>
			&nbsp;|&nbsp; <span id="grab_latest_<?php echo $id ?>"><a href="grabLatestFrom/<?php echo $id ?>" onclick="return grabLatestFrom('<?php echo $config['passphrase'] ?>',<?php echo $id ?>)">Grab Latest</a></span>
			&nbsp;|&nbsp; <?php echo getLastUpdatedHTML($subscription) ?>
		</td>
	</tr>
	<?php
}

mysql_close($link);
?>
</table>
<script type="text/javascript">resizeUrlsToMaxTitleWidth()</script>
<br /><br /><br /><br /><br />
</body>
</html>
